package com.example.smartf.controller;

import com.example.smartf.model.HistoryData;
import com.example.smartf.model.mysql.Deviceinfo;
import com.example.smartf.service.DevService;
import com.example.smartf.tool.influxdb.InfluxdbDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 关于用户与设备之间的对照关系的管理
 * 体现在MongoDB中User表
 * 控制层
 */
@Api("对设备操作")
@RestController
@CrossOrigin(origins = "*")

public class DevController {

    @Autowired
    private DevService devService;
    @Autowired
    private InfluxdbDao influxdbDao;


    //下行控制接口
    @ApiOperation("用户下发控制指令")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名（邮箱号）", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "deviceID", value = "用户操作的设备ID", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "msg", value = "下发指令", dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/dev/downctrl")
    public String DownCtrl(@RequestParam("username") String username,
                           @RequestParam("deviceID") String deviceID,
                           @RequestParam("msg") String msg){
        return devService.DownCtrl(username,deviceID,msg);
    }


    //查询历史记录
    @ApiOperation("查询历史环境记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceID", value = "用户查询的设备ID", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "starttime", value = "起始时间", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "endtime", value = "终止时间", dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/dev/historydata")
    public List<HistoryData> HistoryData(@RequestParam("deviceID") String deviceID,
                                           @RequestParam("starttime") String starttime,
                                           @RequestParam("endtime") String endtime){
        return influxdbDao.findByTime(starttime,endtime,deviceID);
    }


    //查询历史记录
    @ApiOperation("查询设备阈值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceID", value = "修改阈值的设备ID", dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/dev/getthreshold")
    public Deviceinfo getthreshold(@RequestParam("deviceID") String deviceID){
        return devService.Getthreshold(deviceID);
    }

    //查询历史记录
    @ApiOperation("更该设备阈值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceID", value = "修改阈值的设备ID", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type", value = "阈值类型", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "max", value = "阈值上限", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "min", value = "阈值下限", dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/dev/changethreshold")
    public String updatethreshold(@RequestParam("deviceID") String deviceID,
                                             @RequestParam("type") String type,
                                             @RequestParam("max") String max,
                                             @RequestParam("min") String min){
        
        return devService.Updatethreshold(deviceID, type, "null".equals(max) == true ? null:Double.parseDouble(max)
                                                        , "null".equals(min) == true ? null:Double.parseDouble(min));
    }

}
